﻿using BuildPlannerLib.Buildings;
using BuildPlannerLib.PlayerActions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BuildPlannerLib.Terran.PlayerActions
{
    // Derived class from PlayerAction for producing Terran buildings
    public class ProduceTerranBuilding : PlayerAction
    {
        // Field for the building being produced
        public Building Building;

        // Field for the base from which the SCV will be taken
        public Base Base;

        // Constructor with parameters for build and unit
        public ProduceTerranBuilding(Build build, Base b, Building building)
        {
            Build = build;
            Base = b;
            Building = building;
            MineralCost = Building.MineralCost;
            GasCost = Building.GasCost;
            TimeDuration = Building.TimeDuration;
            SupplyProduced = Building.SupplyProduced;
            Description = "Produce " + Building.Name;
            LongDescription = "Producing a new " + Building.Name + ".";
        }

        // Evaluates whether there is mineral income or enough minerals, whether there is gas income or enough gas, 
        // whether the requirement building is started, and whether there is an SCV available to build
        public override bool RequirementsFilled()
        {
            Building requirementBuilding = null;
            if (Building.Requirement != null)
                requirementBuilding = Build.Player.GetBuilding(Building.Requirement);
            Error = null;

            if (requirementBuilding != null && requirementBuilding.Started == 0)
            {
                Error = "The required building \"" + Building.Requirement + "\" has not been built.";
                return false;
            }

            if (Base.MineralWorkers == 0)
            {
                Error = "There are no SCVs at this base to build the building.";
                return false;
            }

            return base.RequirementsFilled();
        }

        // Evaluates if there are enough minerals, enough gas, requirement building is finished, and production facility is not in-use
        public override bool StartCheck()
        {
            Building requirementBuilding = null;
            if (Building.Requirement != null)
                requirementBuilding = Build.Player.GetBuilding(Building.Requirement);

            if (requirementBuilding != null && requirementBuilding.GetCount() == 0)
                return false;

            if (Base.MineralWorkerStatus.Sum() == 0)
                return false;

            return base.StartCheck();
        }

        // Spends the minerals, spends the gas, puts the production building in-use and adds the event to finish the action
        public override void StartAction()
        {
            base.StartAction();

            Base.RemoveMineralWorker(ref Build);
            Build.Player.IdleWorkers++;
            Building.Started++;
        }

        // Sets the production facility from being in-use and finishes the upgrade
        public override void FinishAction()
        {
            base.FinishAction();

            Building.AddBuilding();
            Base.AddMineralWorker(ref Build);
            Build.Player.IdleWorkers--;
        }
    }
}
